import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:short_drama/models/message_model.dart';
import 'package:short_drama/pages/home/message_viewmodel.dart';
import 'package:short_drama/utils/colors.dart';
import 'package:short_drama/utils/float.dart';

class MessagePage extends StatefulWidget {
  const MessagePage({super.key});

  @override
  State<MessagePage> createState() => _MessagePageState();
}

class _MessagePageState extends State<MessagePage> {
  MessageViewmodel viewModel = MessageViewmodel();
  final RefreshController _controller = RefreshController();

  @override
  void initState() {
    super.initState();
    firstRefresh();
  }

  firstRefresh() async {
    EasyLoading.show(status: 'loading', maskType: EasyLoadingMaskType.clear);
    await viewModel.queryRefresh();
    EasyLoading.dismiss();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ClientColors.ff0a0f12,
      appBar: AppBar(
        iconTheme: IconThemeData(color: Colors.white),
        centerTitle: false,
        titleSpacing: FloatConstance.navBarSpace,
        leading: IconButton(
          onPressed: () => {Get.back()},
          icon: Icon(Icons.arrow_back_ios),
        ),
        backgroundColor: ClientColors.odffffff,
        title: Text('消息', style: TextStyle(color: Colors.white, fontSize: 18)),
      ),
      body: SmartRefresher(
        controller: _controller,
        header: const ClassicHeader(),
        enablePullUp: true,
        onRefresh: () async {
          await viewModel.queryRefresh();
          _controller.refreshCompleted();
        },
        onLoading: () async {
          await viewModel.queryLoadMore();
          _controller.loadComplete();
        },
        child: Obx(
          () => ListView.builder(
            itemCount: viewModel.messages.length,
            itemBuilder: (context, index) {
              final MessageModel item = viewModel.messages[index];
              return Container(
                margin: EdgeInsets.only(
                  top: (index == 0) ? 20 : 10,
                  left: 16,
                  right: 16,
                ),
                padding: EdgeInsets.all(10),
                decoration: BoxDecoration(
                  color: ClientColors.odffffff,
                  borderRadius: BorderRadius.circular(10),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          item.send_title ?? '',
                          textAlign: TextAlign.left,
                          style: TextStyle(color: Colors.white, fontSize: 15),
                        ),
                        Expanded(child: Container()),
                      ],
                    ),
                    SizedBox(height: 10),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          child: Text(
                            item.send_message ?? '',
                            textAlign: TextAlign.left,
                            style: TextStyle(
                              color: ClientColors.aaffffff,
                              fontSize: 14,
                            ),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: 10),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(child: Container()),
                        Text(
                          item.create_time ?? '',
                          style: TextStyle(
                            color: ClientColors.aaffffff,
                            fontSize: 11,
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}
